<template>
    <el-button type="primary">单击我</el-button>
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="用户名" prop="username">
            <el-input v-model="form.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
            <el-input type="password" v-model="form.password"></el-input>
        </el-form-item>
        <el-form-item>
            <el-button type="primary" @click="onSubmit">提交</el-button>
        </el-form-item>
    </el-form>
</template>
<script setup>
import { ref } from 'vue';
const form = ref({
    username: '',
    password: ''
});
const rules = ref({
    username: [
        { required: true, message: '请输入用户名', trigger: 'blur' },
        { min: 3, max: 15, message: '用户名长度在 3 到 15 个字符', trigger: 'blur' }
    ],
    password: [
        { required: true, message: '请输入密码', trigger: 'blur' },
        { min: 6, message: '密码长度不能小于 6 个字符', trigger: 'blur' }
    ]
});
const formRef = ref(null);
const onSubmit = () => {
    formRef.value.validate((valid) => {
        if (valid) {
            alert('提交成功！');
        } else {
            alert('表单验证失败');
            return false;
        }
    });
}; 
</script>